home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / arg2str.c next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  3.6 KB  |  184 lines

  1. #include "util.h"
  2.  
  3. /* convert an array of strings to one or more lines of 'arguments'.
  4.  *
  5.  * this is intended to be used along with the str2arg() routine.
  6.  */
  7.  
  8. /*VARARGS*/
  9. #if sparc
  10. #include <varargs.h>
  11. #define MAXARGS 20
  12. arg2lstr (va_alist)
  13. va_dcl
  14. {
  15.   va_list ap;
  16.   int linelen, maxlen;
  17.   char *buf;
  18.   char *argv[MAXARGS];
  19.   register int i;
  20.  
  21.   va_start(ap);
  22.   linelen = va_arg(ap, int);
  23.   maxlen = va_arg(ap, int);
  24.   buf = va_arg(ap, char *);
  25.   for (i = 0; i < MAXARGS; ++i)
  26.     {
  27.       if ((argv[i] = va_arg(ap, char *)) == (char *)0)
  28.     break;
  29.     }
  30.   va_end(ap);
  31.   arg2vstr (linelen, maxlen, buf, argv);
  32. }
  33. #else /* sparc */
  34. #ifdef NO_VARARGS
  35. arg2lstr (linelen, maxlen, buf, arg1, a,b,c,d,e,f,g,h,i,j,k,l,m)
  36. #else
  37. arg2lstr (linelen, maxlen, buf, arg1) /* convert list to argument array */
  38. #endif /* NO_VARARGS */
  39.     int linelen;        /* when to insert newlines; 0=> don't       */
  40.     int maxlen;
  41.     char *buf;
  42.     char *arg1;        /* merely the first of the list            */
  43. {
  44.     arg2vstr (linelen, maxlen, buf, &arg1);
  45. }
  46. #endif /* sparc */
  47.  
  48. arg2vstr (linelen, maxlen, buf, argv) /* convert the list to a string            */
  49.     int linelen;
  50.     int maxlen;    /* length of string            */
  51.     char *buf;    /* where to put the output string    */
  52.     char **argv;    /* the argument vector            */
  53. {
  54.     unsigned totlen,    /* total length of current line         */
  55.          len;       /* length of current argument           */
  56.     unsigned gotdelim;       /* a delimiter char is in arg           */
  57.     unsigned gotpair;        /* key/value pair                       */
  58.     char tmpstr[256];    /* LINESIZE = 256, string under construction  */
  59.     register char *src,
  60.           *dest;
  61.  
  62.     for (totlen = gotpair = 0, buf[0] = '\0';
  63.         *argv != (char *) 0; argv++)
  64.     {
  65.     if (gotpair  == 0 && strcmp ("=", *argv) == 0)
  66.     {
  67.         dest = tmpstr;
  68.         gotpair = 2;     /* take the next two arguments  */
  69.         continue;
  70.     }
  71.  
  72.     for (src = *argv, gotdelim = FALSE; *src != '\0'; src++)
  73.         switch (*src)
  74.         {
  75.         case ' ':
  76.         case '\t':
  77.         case '=':
  78.         case ',':
  79.         case ';':
  80.         case ':':
  81.         case '/':
  82.         case '|':
  83.         case '.':
  84.             gotdelim = TRUE;
  85.             goto nextone;
  86.         }
  87.  
  88.     nextone:
  89.     if (gotpair == 0)
  90.         dest = tmpstr;
  91.     if (gotdelim)
  92.         *dest++ = '"';
  93.     for (src = *argv; *src != '\0'; src++)
  94.     {
  95.         switch (*src)
  96.         {
  97.         case '\b':
  98.             *dest++ = '\\';
  99.             *dest++ = 'b';
  100.             break;
  101.  
  102.         case '\t':
  103.             *dest++ = '\\';
  104.             *dest++ = 't';
  105.             break;
  106.  
  107.         case '\f':
  108.             *dest++ = '\\';
  109.             *dest++ = 'f';
  110.             break;
  111.  
  112.         case '\r':
  113.             *dest++ = '\\';
  114.             *dest++ = 'r';
  115.             break;
  116.  
  117.         case '\n':
  118.             *dest++ = '\\';
  119.             *dest++ = 'n';
  120.             break;
  121.  
  122.         case '\\':        /*  Added by Doug Kingston  */
  123.         case '\"':
  124.             *dest++ = '\\';
  125.             *dest++ = *src;
  126.             break;
  127.  
  128.         default:
  129.             if (iscntrl (*src))
  130.             {
  131.             *dest++ = '\\';
  132.             *dest++ = ((*src >> 6) & 07) + '0';
  133.             *dest++ = ((*src >> 3) & 07) + '0';
  134.             *dest++ =  (*src & 07) + '0';
  135.             }
  136.             else
  137.             *dest++ = *src;
  138.         }
  139.     }
  140.  
  141.     if (gotdelim)
  142.         *dest++ = '"';
  143.  
  144.     if (src == *argv)       /* null-valued argument data            */
  145.     {
  146.         if (totlen != 0)    /* beyond the beginning, so double it   */
  147.         strcat (buf, ",");
  148.         else
  149.         strcat (buf, " ");
  150.         *dest++ = ',';
  151.     }
  152.  
  153.     switch (gotpair)        /* handle key/value differently         */
  154.     {
  155.         case 2:
  156.         *dest++ = '=';
  157.         gotpair--;
  158.         continue;
  159.  
  160.         case 1:
  161.         gotpair = 0;
  162.     }
  163.  
  164.     *dest = '\0';
  165.     len = dest - tmpstr;
  166.  
  167.     if (totlen != 0)
  168.     {
  169.         if ((linelen > 0) && ((totlen + len) > linelen))
  170.         {
  171.         strcat (buf, "\n\t");
  172.         totlen = 8;
  173.         }    
  174.         else
  175.         {
  176.         strcat (buf, " ");
  177.         totlen += 1;
  178.         }
  179.     }
  180.     strcat (buf, tmpstr);
  181.     totlen += len;
  182.     }
  183. }
  184.